package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;

/**
 * The SearchResult class takes search information from "SearchServlet" and
 * makes a query with it and returns a ResultSet object.
 * 
 * @author Olof Ekelund.
 */

public class SearchResult {
    Connection con;
    ResultSet rs;
    String searchString;
    int pages;

    public SearchResult() {

    }

    /**
     * Constructor makes a DB-connection and creates a resultset from the
     * searchword.
     */

    public SearchResult(String searchString, String[] types, String meal,
	    String weekday, int maxPrice) {
	this.searchString = searchString;

	if (weekday.equals("today")) {
	    Calendar cal = Calendar.getInstance();
	    java.util.Date utilDate = cal.getTime();
	    java.sql.Date today = new Date(utilDate.getTime());

	    weekday = today.toString();
	}

	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	} catch (Exception e) {
	    System.out.println(e);
	}
	try {
	    Statement stmt = con
		    .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
			    ResultSet.CONCUR_READ_ONLY);

	    String query = "SELECT DISTINCT menu.rest_id, menu.dish, menu.description, menu.price "
		    + "FROM olofek."
		    + meal
		    + " AS menu"
		    + " WHERE (menu.dish like '%"
		    + searchString
		    + "%' "
		    + "OR menu.description like '%"
		    + searchString
		    + "%') "
		    + "AND menu.price <= " + maxPrice + " ";

	    // Add the foodtypes to the query.
	    if (types != null && types.length != 0) {
		query += "AND menu.type IN (";
		for (int i = 0; i < types.length; i++) {
		    if (i < types.length - 1)
			query += "'" + types[i] + "', ";
		    else
			query += "'" + types[i] + "') ";
		}
	    }

	    // Add the date.
	    if (meal.equals("lunch")) {
		if (!(weekday.equals("null"))) {
		    query += " AND date = '" + weekday + "' ";
		}
	    }

	    query += "ORDER BY menu.rest_id";

	    rs = stmt.executeQuery(query);
	} catch (Exception e) {
	    System.out.println(e);
	}
	try {
	    while (rs.next()) {
		pages++;
	    }

	    rs.beforeFirst();

	    if ((float) pages % 10 == 0)
		pages = pages / 10;
	    else
		pages = pages / 10 + 1;

	} catch (Exception e) {

	}
    }

    /**
     * Update table with searchword and increment noofsearches field
     */

    public void updateTable() {
	try {
	    Statement stmt = con.createStatement();

	    String query = "INSERT INTO olofek.searches (keyword, noofsearches)"
		    + " VALUES ('"
		    + searchString
		    + "', 1)"
		    + "ON DUPLICATE KEY UPDATE noofsearches = noofsearches + 1;";

	    stmt.executeUpdate(query);
	} catch (Exception e) {
	    System.out.println(e);
	}
    }

    public Connection getCon() {
	return con;
    }

    public void setCon(Connection con) {
	this.con = con;
    }

    public ResultSet getResultSet() {
	return rs;
    }

    public int getPages() {
	return pages;
    }
}